package com.wjjun.basemvpvm.data.repository;

import com.wjjun.basemvpvm.data.exception.MyException;
import com.wjjun.basemvpvm.data.model.PageEntity;
import com.wjjun.basemvpvm.data.model.SimpleEntity;
import com.wjjun.basemvpvm.data.net.HttpApiManager;
import com.wjjun.basemvpvm.data.net.filter.BaseResponseFilter;
import com.wjjun.basemvpvm.data.response.BaseResponse;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

/**
 * 基础的api仓库类，主要处理网络请求
 * author Wjjun
 * Created 2018/3/20.
 */

public abstract class BaseApiRepository {

    protected HttpApiManager httpApiManager;
    private BaseResponseFilter filter;

    public BaseApiRepository(HttpApiManager httpApiManager) {
        this.httpApiManager = httpApiManager;
    }

    public BaseApiRepository(HttpApiManager httpApiManager, BaseResponseFilter filter) {
        this.httpApiManager = httpApiManager;
        this.filter = filter;
    }

    @SuppressWarnings("unchecked")
    protected <T extends Observable<? extends BaseResponse>, E> Observable<E> res2Entity(T t) {
        return t.flatMap(new Function<BaseResponse, ObservableSource<E>>() {
            @Override
            public ObservableSource<E> apply(@NonNull BaseResponse baseResponse) {

                //被拦截后按错误处理
                if (filter != null && filter.onFilter(baseResponse)) {
                    return Observable.error(new MyException(baseResponse.getErrorCode(), ""));
                }

                if (!baseResponse.isSuccess()) {    //接口请求失败
                    return Observable.error(new MyException(baseResponse.getErrorCode(), baseResponse.getMessage()));
                }

                if (baseResponse.getEntity() == null) {  //请求成功,但是没有数据
                    return Observable.error(new MyException(baseResponse.getErrorCode(), baseResponse.getMessage()));

                } else if (baseResponse.getPageIndex() >= 0) {   //可以分页
                    return Observable.just((E) new PageEntity<>((E) baseResponse.getEntity(),
                            baseResponse.getPageIndex(),
                            baseResponse.getTotalPage(),
                            baseResponse.getPageSize()));
                }

                return Observable.just((E) baseResponse.getEntity());
            }
        });
    }

    @SuppressWarnings("unchecked")
    protected <T extends Observable<? extends BaseResponse>, E> Observable<E> res2SimpleEntity(T t) {
        return t.flatMap(new Function<BaseResponse, ObservableSource<E>>() {
            @Override
            public ObservableSource<E> apply(@NonNull BaseResponse baseResponse) {

                //被拦截后按错误处理
                if (filter != null && filter.onFilter(baseResponse)) {
                    return Observable.error(new MyException(baseResponse.getErrorCode(), ""));
                }

                if (!baseResponse.isSuccess()) {    //接口请求失败
                    return Observable.error(new MyException(baseResponse.getErrorCode(), baseResponse.getMessage()));
                }

                if (baseResponse.getEntity() == null) {  //请求成功,但是没有数据
                    baseResponse.setData(new SimpleEntity(baseResponse.getErrorCode(), baseResponse.getMessage()));

                } else if (baseResponse.getPageIndex() >= 0) {   //可以分页
                    return Observable.just((E) new PageEntity<>((E) baseResponse.getEntity(),
                            baseResponse.getPageIndex(),
                            baseResponse.getTotalPage(),
                            baseResponse.getPageSize()));
                }

                return Observable.just((E) baseResponse.getEntity());
            }
        });
    }

}
